package Index;

use strict;

use Clair::Utils::CorpusDownload;
use Clair::Utils::Tf;

sub create_meta_dbm {
  my $meta_dir='metadata';
  if (-d $meta_dir) {
    system ("rm -rf $meta_dir");
  }
  # create db files to store meta data in each XML file
  system("mkdir $meta_dir");
  my %title_meta=();
  dbmopen(%title_meta, "$meta_dir/titles", 0666);
  my %bibl_meta=();
  dbmopen(%bibl_meta, "$meta_dir/bibl", 0666);
  my %author_meta=();
  dbmopen(%author_meta, "$meta_dir/authors", 0666);
  
  my $hash_array = [ \%title_meta, \%bibl_meta,
                     \%author_meta ];
  return $hash_array;
}

sub close_meta_dbm {
  my $meta_hash = shift;
  my $meta_dir='metadata';

  my $temp = $meta_hash->[0];
  dbmclose %$temp;
  $temp = $meta_hash->[1];
  dbmclose %$temp;
  $temp = $meta_hash->[2];
  dbmclose %$temp;
}

sub store_metadata {
  my $hash = shift;
  my $meta_hash = shift;
  my $docno = $hash->{"DOCNO"};
  $docno =~ s/[^0-9]*([0-9]+)[^0-9]*/$1/g;

  $meta_hash->[0]->{$docno} = $hash->{"TITLE"};
  $meta_hash->[1]->{$docno} = $hash->{"BIBLIO"};
  $meta_hash->[2]->{$docno} = $hash->{"AUTHOR"};
}

sub find_title {
  my $docid = shift;
  chomp $docid;

  my %buffer = ();
  dbmopen(%buffer, "metadata/titles", 0) || die "Could not open dbm file \"metadata/title\"";

  my $title = $buffer{$docid};
  if (defined $title) {
    print "[title of doc #$docid]:\n$title\n";
  } else {
    print "Cannot find doc with ID = $docid\n";
  }

  dbmclose %buffer;
}

sub find_fulltext {
  my $docid = shift;
  chomp $docid;
  
  print "[full text of doc #$docid]:\n";
  system("more txt_corpus/$docid.txt");
}

sub create_index {
  my $data_source = shift;
  # rootdir is the root of the dir tree that storing one or more corpora
  my $corpus = Clair::Utils::CorpusDownload->new(corpusname=>"cranfieldcorpus",
                                                 rootdir=> "produced");
  $corpus->build_corpus_from_directory(dir => "$data_source", 
                                       cleanup => 0, skipCopy => 0);
  $corpus->buildIdf(stemmed=>1);
  $corpus->build_docno_dbm();
  $corpus->buildTf(stemmed=>1);
  $corpus->build_term_counts(stemmed=>1);
}

1;

